package com.ay.weixin.fdc.boss.user.domain;

import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import org.lizar.json.JObject;
import org.lizar.util.StringHelper;

public class Activity {

	
	
	/**
	 * 
	 * 根据规则进行数字抽奖
	 * 
	 * 例如:1~1.5:0.7,1.5~3:0.3
	 * 
	 * 那么得到的结果是,1~1.5之间的数概率为70%，1.5~3之间的数概率为30%
	 * 
	 * */
	public static String activity_cash_rules(String rough_rules){
		System.out.println("AR record ount: get rules "+rough_rules);
		if(StringHelper.isNull(rough_rules))return "0";
		Random rnd=new Random();
		double r=0.0;
		r=Math.random();
		if(rough_rules.indexOf(",")==-1){
			System.out.println("AR get last rule");
			int ri=rough_rules.indexOf("~");
			if(ri==-1){
				System.out.println("AR return result:"+rough_rules);
				return rough_rules;
			}else{
				int r1=(int)(Double.parseDouble(rough_rules.substring(0, ri))*100);
				int r2=(int)(Double.parseDouble(rough_rules.substring(ri+1))*100);
				System.out.println("AR return result:"+((rnd.nextInt((r2-r1))+r1)/100.0));
				return ((rnd.nextInt((r2-r1))+r1)/100.0)+"";
			}
		}else{
			double r2=0.0;
			String[] rgs=rough_rules.split(",");
			for(String rs:rgs){
				System.out.println("AR test rule with "+rs);
				int rindex=rs.indexOf(":");
				if(rindex==-1){
					System.out.println("AR return result "+rs);
					return rs;
				}
				if(rs.indexOf("~")==-1){
					System.out.println("AR return result "+rs);
					return rs.substring(0, rs.indexOf("~"));
				}
				r2+=Double.parseDouble(rs.substring(rindex+1));
				if(r<r2){
					String[] reg=rs.substring(0, rindex).split("~");
					if(reg.length!=2)return rs.substring(0, rindex);
					int range1=(int)(Double.parseDouble(reg[0])*100);
					int range2=(int)(Double.parseDouble(reg[1])*100);
					System.out.println("AR return result "+((rnd.nextInt((range2-range1))+range1)/100.0));
					return ((rnd.nextInt((range2-range1))+range1)/100.0)+"";
				}
			}
		}
		return "0";
	}
	
}
